#-*- encoding: iso8859-1 -*-
import urllib
import urllib2
import cookielib
import time
from datetime import datetime, timedelta


def get_html_source(theurl,txdata = None,txheaders = {'User-agent' : 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'}, cj = cookielib.CookieJar(), l_proxy_info = ""):
    if (len(l_proxy_info)>0):
        l_proxy_support = urllib2.ProxyHandler({"http" : \
        "http://%(host)s:%(port)d" %
        l_proxy_info})
        print l_proxy_info
        opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj),l_proxy_support)
    else:
        opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
    urllib2.install_opener(opener)
    source = ""
    try:
        req = urllib2.Request(theurl, txdata, txheaders)
        handle = urllib2.urlopen(req,timeout=5)
    except IOError, e:
        source = 'Falha em abrir a URL "%s".' % theurl
        if hasattr(e, 'code'):
            source = source + ('Codigo do erro - %s.' % e.code)
    else:
        source = handle.read().strip()
    
    """    
    print 'Cookies recebidos:'
    for index, cookie in enumerate(cj):
        print index, '  :  ', cookie   
        #handle["Set-Cookie"] = "name=tom"
    #cj.save()  
    """  
    return source,cj

def get_all_proxy51(api_all_proxy_url='http://www.proxy51.com/api/f9448939cd187cbd7f921f1642e2d826'):
    form,cj = get_html_source(api_all_proxy_url)
    return form.split("\n")

def get_all_proxy51_formatted(api_all_proxy_url='http://www.proxy51.com/api/f9448939cd187cbd7f921f1642e2d826'):
    form,cj = get_html_source(api_all_proxy_url)
    one_str_formatted = []
    for proxy_random in form.split("\n"):
        #print proxy_random
        one_str_formatted.append(proxy_random.strip().split(":")[-2]+":"+proxy_random.strip().split(":")[-1]+"@"+proxy_random.strip().split(":")[0]+":"+proxy_random.strip().split(":")[1])
        
    return one_str_formatted

if __name__ == '__main__':
    
    api_all_proxy_url = 'http://www.proxy51.com/api/f9448939cd187cbd7f921f1642e2d826'
    
    form,cj = get_html_source(api_all_proxy_url)
    print form.split("\n")
    
    for proxy_url in form.split("\n"):
        print proxy_url